//
//  GAOEmoticonTools.swift
//  Weibo28
//
//  Created by 高辰 on 16/5/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class GAOEmoticonTools: NSObject {
    // 单例全局访问点
    static let shareTools: GAOEmoticonTools = GAOEmoticonTools()
    // 私有化构造函数
    private override init() {
        super.init()
    }
    
    //创建emotionbundle对象
    private lazy var emotionBundle: NSBundle = {
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        let bundle = NSBundle(path: path)!
        return bundle
    }()
    
    //  读取默认表情
    private lazy var defaultEmoticons: [GAOEmoticonModel] = {
        return self.loadEmoticonsWithPath("default/info.plist")
    }()
    
    //  读取emoji表情
    private lazy var emojiEmoticons: [GAOEmoticonModel] = {
        return self.loadEmoticonsWithPath("emoji/info.plist")
    }()
    
    //  浪小花
    private lazy var lxhEmoticons: [GAOEmoticonModel] = {
        return self.loadEmoticonsWithPath("lxh/info.plist")
    }()
    
    ///  提供给表情视图的数组
    lazy var allEmoticons: [[[GAOEmoticonModel]]] = {
        //  表情视图包含三个种类型的二维数组
        return [
            self.sectionWithEmotions(self.defaultEmoticons),
            self.sectionWithEmotions(self.emojiEmoticons),
            self.sectionWithEmotions(self.lxhEmoticons)
        ]
    }()
    
    // 抽取读取表情数据方法
    private func loadEmoticonsWithPath(subPath: String) -> [GAOEmoticonModel] {
        let path = self.emotionBundle.pathForResource(subPath, ofType: nil)!
        // 加载plist资源数据
        let array = NSArray(contentsOfFile: path)!
        
        // 遍历数组转模型
        var modelArray = [GAOEmoticonModel]()
        for dict in array {
            let emotion = GAOEmoticonModel(dict: dict as! [String : AnyObject])
            // 判断表情类型是否是图片
            if emotion.type == "0" {
                // 获取图片名字
                let pngName = emotion.png!
                // 给imagePath拼接路径
                emotion.imagePath = (path as NSString).stringByReplacingOccurrencesOfString("info.plist", withString: pngName)
            }
            
            modelArray.append(emotion)
        }
        return modelArray
    }
    
    // 计算各种表情显示的页数,截取数据
    private func sectionWithEmotions(emoticons: [GAOEmoticonModel]) -> [[GAOEmoticonModel]] {
        // 计算页数
        let pageCount = (emoticons.count - 1) / 20 + 1
        
        var tempArray: [[GAOEmoticonModel]] = [[GAOEmoticonModel]]()
        
        // 遍历页数计算截取
        for i in 0..<pageCount {
            
            // 计算当前截取位置的索引
            let locaction = i * 20
            var length = 20
            //  如果数组越界 重写计算截取个数
            if locaction + length > emoticons.count {
                length = emoticons.count - locaction
            }
            
            let subArray = (emoticons as NSArray).subarrayWithRange(NSMakeRange(locaction, length)) as! [GAOEmoticonModel]
            tempArray.append(subArray)
        }
        return tempArray
    }
    
    //  通过表情字符串 查找表情模型
    func emoticonWithEmoticonStr(emoticonStr: String) -> GAOEmoticonModel? {
        //  先从默认表数组中查找
        for value in defaultEmoticons {
            if value.chs == emoticonStr {
                return value
            }
        }
        //  从浪小花数组中查找
        for value in lxhEmoticons {
            if value.chs == emoticonStr {
                return value
            }
        }
        
        
        return nil
        
    }



    
}
